<?php

class stats extends a_admin_controller {
    protected $srv;
    protected $ga;

    protected $date_range;
    protected $is_per_day;

    protected $page_visits;
    protected $page_views;

    public function __construct() {
        parent::__construct(true);
        $this->srv = new service_l_analytics;
        $this->getResults =NULL;
        $this->is_per_day =1;

    }

    public function batch_assign(){
        $this->smarty()->assign("ga", $this->ga);

        $this->smarty()->assign("numeric_month", service_l_analytics::get_calendar()->numeric_month);
        $this->smarty()->assign("calender_month", service_l_analytics::get_calendar()->calendar_month);

        $this->smarty()->assign("date_range", $this->date_range);

        $this->smarty()->assign("is_per_day", $this->is_per_day);
        
        $this->smarty()->assign("page_visits", $this->page_visits);
        $this->smarty()->assign("page_views", $this->page_views);
    }

    public function index()
    {
        $start = trim($this->input->post("start_date"));
        $end = trim($this->input->post("end_date"));

        $this->date_range = $this->srv->set_date($start, $end);

        strcmp($this->input->post("filter_by"), "month") ? $this->is_per_day = 1 : $this->is_per_day = 0;
        
        $this->ga_setup($this->date_range->ga->start, $this->date_range->ga->end);
    }

    private function ga_setup($start, $end)
    {
        $this->ga = new stdClass();
        $this->ga = service_l_analytics::ga_connect();

        $this->ga = $this->srv->ga_request($this->ga, $start, $end, $this->is_per_day);

        $array = service_l_analytics::sort_array($this->srv->get_data($this->ga->getResults(), $this->is_per_day));

        $array = $this->srv->split_array($array);

        $this->page_visits = json_encode($array[0]);
        $this->page_views = json_encode($array[1]);
    }

}

?>